<template>
  <div class="border-box" :class="[`border-box-${type}`, { reverse }]" :style="containerStyle">
    <!-- 装饰性边框元素 -->
    <div class="border-decorations">
      <!-- 四个角的装饰 -->
      <div class="corner corner-tl"></div>
      <div class="corner corner-tr"></div>
      <div class="corner corner-bl"></div>
      <div class="corner corner-br"></div>

      <!-- 边框线条 -->
      <div class="border-line border-top"></div>
      <div class="border-line border-right"></div>
      <div class="border-line border-bottom"></div>
      <div class="border-line border-left"></div>
    </div>

    <div class="border-content" :style="contentStyle">
      <slot></slot>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'

// Props定义
const props = defineProps({
  type: {
    type: Number,
    default: 1,
    validator: (value) => [1, 2, 3].includes(value)
  },
  width: {
    type: [String, Number],
    default: '100%'
  },
  height: {
    type: [String, Number],
    default: '100%'
  },
  colors: {
    type: Array,
    default: () => ['#4fd2dd', '#235fa7']
  },
  reverse: {
    type: Boolean,
    default: false
  },
  backgroundColor: {
    type: String,
    default: 'rgba(0, 0, 0, 0.3)'
  },
  padding: {
    type: [String, Number],
    default: 20
  }
})

// 计算属性
const containerStyle = computed(() => ({
  width: typeof props.width === 'number' ? props.width + 'px' : props.width,
  height: typeof props.height === 'number' ? props.height + 'px' : props.height,
  backgroundColor: props.backgroundColor,
  '--primary-color': props.colors[0],
  '--secondary-color': props.colors[1]
}))

const contentStyle = computed(() => ({
  padding: typeof props.padding === 'number' ? props.padding + 'px' : props.padding
}))
</script>

<style scoped>
.border-box {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
}

.border-decorations {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 1;
}

.corner {
  position: absolute;
  width: 20px;
  height: 20px;
  border: 2px solid var(--primary-color);
}

.corner-tl {
  top: 0;
  left: 0;
  border-right: none;
  border-bottom: none;
  animation: cornerGlow 2s ease-in-out infinite alternate;
}

.corner-tr {
  top: 0;
  right: 0;
  border-left: none;
  border-bottom: none;
  animation: cornerGlow 2s ease-in-out infinite alternate 0.5s;
}

.corner-bl {
  bottom: 0;
  left: 0;
  border-right: none;
  border-top: none;
  animation: cornerGlow 2s ease-in-out infinite alternate 1s;
}

.corner-br {
  bottom: 0;
  right: 0;
  border-left: none;
  border-top: none;
  animation: cornerGlow 2s ease-in-out infinite alternate 1.5s;
}

.border-line {
  position: absolute;
  background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
  opacity: 0.6;
}

.border-top {
  top: 0;
  left: 20px;
  right: 20px;
  height: 2px;
  animation: lineFlow 3s linear infinite;
}

.border-right {
  top: 20px;
  right: 0;
  bottom: 20px;
  width: 2px;
  background: linear-gradient(180deg, var(--primary-color), var(--secondary-color));
  animation: lineFlow 3s linear infinite 0.75s;
}

.border-bottom {
  bottom: 0;
  left: 20px;
  right: 20px;
  height: 2px;
  background: linear-gradient(270deg, var(--primary-color), var(--secondary-color));
  animation: lineFlow 3s linear infinite 1.5s;
}

.border-left {
  top: 20px;
  left: 0;
  bottom: 20px;
  width: 2px;
  background: linear-gradient(0deg, var(--primary-color), var(--secondary-color));
  animation: lineFlow 3s linear infinite 2.25s;
}

.border-content {
  position: relative;
  width: 100%;
  height: 100%;
  z-index: 2;
}

@keyframes cornerGlow {
  0% {
    box-shadow: 0 0 5px var(--primary-color);
    border-color: var(--primary-color);
  }
  100% {
    box-shadow: 0 0 15px var(--secondary-color);
    border-color: var(--secondary-color);
  }
}

@keyframes lineFlow {
  0% {
    opacity: 0.3;
    transform: scale(1);
  }
  50% {
    opacity: 0.8;
    transform: scale(1.02);
  }
  100% {
    opacity: 0.3;
    transform: scale(1);
  }
}

/* 不同类型的特殊样式 */
.border-box-1 {
  border-radius: 0;
}

.border-box-1 .corner {
  border-radius: 0;
}

.border-box-2 {
  border-radius: 12px;
}

.border-box-2 .corner {
  border-radius: 4px;
}

.border-box-3 {
  clip-path: polygon(15px 0, 100% 0, 100% calc(100% - 15px), calc(100% - 15px) 100%, 0 100%, 0 15px);
}

.border-box-3 .corner {
  display: none;
}

.border-box.reverse .border-line {
  animation-direction: reverse;
}

/* 悬停效果 */
.border-box:hover .corner {
  animation-duration: 1s;
}

.border-box:hover .border-line {
  opacity: 1;
  animation-duration: 1.5s;
}
</style>
